<?php
@session_start();
require_once('../config/config.php');
require_once('../utils/CommonUtils.php');
require_once('../utils/DBUtils.php');
include ("../utils/Constants.php");
require_once('../utils/BaseDAO.php');
require("../utils/Session.php");
require_once('../utils/Uploader.php');

$commonUtils = new CommonUtils();
$uploader = new Uploader();

$onlineUser = Session::get('fullname');
if($onlineUser == null){
	$commonUtils->redirect("../login.php"); 
}
$baseDAO = new BaseDAO();

$action = $commonUtils->getParameter("action");
$title = $commonUtils->getParameter("title");
$createddate = $commonUtils->getDate();
$updateddate = $commonUtils->getDate();
$status = $commonUtils->getParameter("status");
$createdby = Session::get('userid');
$updatedby = Session::get('userid');
$id = $commonUtils->getParameter("id");
$imgfile = (isset($_FILES['vImage']['name']) ? $_FILES['vImage']['name'] : "");

if (isset($action) && $action == 'insert-update') {
	if(isset($title) && $title != ""){
	 		//process image icon
			if ($imgfile != "") {
				include ('../utils/class.upload.php');
				$imgUpload = new imgUpload($IMG_THUMB_PATH, $IMG_BIG_PATH, $IMG_TEMP_PATH, 100);
				$imgInfos = $commonUtils->uploadAndResizeImage($imgUpload, 150, 600);
				$imgfile = $imgInfos[0];
				$imgwidth = $imgInfos[1];
				$imgheight = $imgInfos[2];
			}
			// end
	 		if(isset($id) && $id > 0){
	 			if($imgfile != ""){
	 				$values = array($id, $title, $imgfile, $updateddate, $updatedby, $status, $imgwidth, $imgheight);
		 			$retval = $baseDAO->update("image", array("id", "title", "image", "updateddate", "updatedby", "status", "width", "height"), $values);
	 			}else{
		 			$values = array($id, $title, $updateddate, $updatedby, $status);
		 			$retval = $baseDAO->update("image", array("id", "title", "updateddate", "updatedby", "status"), $values);
	 			}
	 			$message = "Cập nhật thành công";
	 		}else{
	 			if($imgfile != ""){
	 				$values = array($title, $imgfile, $createddate, $updateddate, $createdby, $updatedby, $status, $imgwidth, $imgheight);
	 				$id = $retval = $baseDAO->create("image", array("title", "image", "createddate", "updateddate", "createdby", "updatedby", "status", "width", "height"), $values);
	 				$message = "Thêm mới thành công";	
	 			}else{
	 				$message = "Vui lòng nhập đủ các thông tin bắt buộc";
	 			}
	 		}
	 		
			if(!$retval){
			  die('Could not insert or update data: ' . mysql_error());
			  exit;
			}
				
	 	}else{
	 		$message = "Vui lòng nhập đủ các thông tin bắt buộc";
	 	}
}

if($id != null && $id > 0){
	$result = $baseDAO->getItem("image", array("id", "title", "image", "status"), $id);
	if (!$result) {
	    echo 'Could not run query: ' . mysql_error();
	    exit;
	}
	$title = $result['title'];
	$image = $result['image']; 
	$status = $result['status'];
}
$categories = $baseDAO->getItems("category", array("id", "name"));
?>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=100" >
		<title>Nguyễn Đậu - Quản lý hình ảnh</title>
		<link rel="stylesheet" type="text/css" href="../css/diary.css"/>
		<script src="../js/jquery-1.7.2.min.js"></script>
		<script type="text/javascript" src="../js/tiny_mce/jquery.tinymce.js"></script>
		<script type="text/javascript" src="http://www.google.com/jsapi"></script>
	</head>
	<body>
		<?php include '../include/header.php';?>
		<center>
			<div class="content">
				<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="formImage" enctype="multipart/form-data">
					<div class="tableHeader">
						Quản lý hình ảnh
					</div>
					<?php if(isset($message)){?>
						<div class="infomessage"><?php echo $message; ?></div>
					<?php }?>
					<table>
						<tr>
							<td width="150px;">Tiêu đề<span class="required">*</span></td>
							<td><input type="text" name="title" value="<?php if(isset($title)){echo $title;}?>"/></td>
						</tr>
						<tr>
							<td>Hiển thị</td>
							<td>
								<input type="radio" name="status" value="1" <?php if(!isset($status) || $status == 1){echo "checked";}?>/>Có
								<input type="radio" name="status" value="0" <?php if(isset($status) && $status == 0){echo "checked";}?>/>Không
							</td>
						</tr>
						<tr>
							<td>Hình ảnh<span class="required">*</span></td>
							<td>
								<input type="file" name="vImage" id="vImage" />
								<?php if(isset($image)){?>
									<br/><img src="../images/images/thumb/<?php echo $image;?>" width="150px"/>
								<?php }?>
							</td>
						</tr>
						<tr>
							<td></td>
							<td>
								<input type="submit" value="<?php if(isset($id)){echo 'Cập nhật';}else{echo 'Lưu';}?>"/>
								<input type="button" value="Trở vể" onclick="window.location='./image-list.php'"/>
							</td>
						</tr>
					</table>
					<input type="hidden" name="id" value="<?php if(isset($id)){echo $id;}?>"/>
					<input type="hidden" name="action" value="insert-update">
				</form>		
			</div>
		</center>
		<?php include '../include/footer.php';?>
	</body>
</html>

